package com.huaweicloud.controller;

import com.alibaba.fastjson.JSON;
import com.huaweicloud.commons.outhUtils.RequestUtil;
import com.huaweicloud.commons.pojo.OBSStorageParams;
import com.huaweicloud.commons.response.Result;
import com.huaweicloud.service.impl.OBSServiceImpl;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;


/**
 * 操作OBS接口OBSController类，如上传一个文件 upLoadOne()
 */
@CrossOrigin
@RestController
@Log4j2
@RequestMapping("/obs")
public class OBSController {

    @Autowired
    public OBSServiceImpl obsService;
    @Autowired
    RequestUtil requestUtil;

    /**
     * @param file
     * @param obsParamsJson
     * @return
     * @throws IOException
     */
    @PostMapping(value = "/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public String upLoadOneFile(@RequestPart("file") MultipartFile file, @RequestParam("obsParamsJson") String obsParamsJson) throws IOException {
        OBSStorageParams obsParams = JSON.parseObject(obsParamsJson, OBSStorageParams.class);
        String objURL = obsService.uploadOneFile(file.getInputStream(), obsParams);
        return objURL;
    }

    /**
     * @param deleobjectname
     * @return
     */
    @PostMapping("/delete")
    public Result deleteOneFile(@RequestBody OBSStorageParams obsParams, @RequestParam("deleobjectname") String deleobjectname) {

        Result result;

        result = obsService.deleteOneFile(deleobjectname, obsParams);

        return result;

    }
}
